#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调试客服消息发送问题
"""

import os
import sys
import logging
import requests
from dotenv import load_dotenv

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from config.settings import settings
from services.wx_api import WeChatAPI

logging.basicConfig(level=logging.INFO)

def debug_customer_service():
    print("🔍 开始调试客服服务问题...")
    
    load_dotenv()
    os.environ['WX_CORPID'] = settings.WX_CORPID
    os.environ['WX_CORPSECRET'] = settings.WX_CORPSECRET
    
    wx_api = WeChatAPI()
    access_token = wx_api.get_access_token()
    if not access_token:
        print("❌ 无法获取access_token")
        return
    
    print(f"✅ access_token: {access_token[:20]}***")
    
    # 检查客服账号配置
    print("\n📋 检查客服账号配置...")
    url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/list"
    params = {'access_token': access_token}
    
    try:
        resp = requests.get(url, params=params, timeout=10)
        print(f"状态码: {resp.status_code}")
        print(f"响应: {resp.text}")
    except Exception as e:
        print(f"请求异常: {str(e)}")
    
    # 检查客服人员状态
    print("\n👥 检查客服人员状态...")
    open_kfid = settings.OPEN_KF_ID
    url = f"https://qyapi.weixin.qq.com/cgi-bin/kf/servicer/list"
    params = {'access_token': access_token, 'open_kfid': open_kfid}
    
    try:
        resp = requests.get(url, params=params, timeout=10)
        print(f"状态码: {resp.status_code}")
        print(f"响应: {resp.text}")
    except Exception as e:
        print(f"请求异常: {str(e)}")
    
    print("\n🎯 调试完成!")

if __name__ == "__main__":
    debug_customer_service() 